
package ch.hearc.coursJava.IO.persistence.preference;

import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.prefs.Preferences;

import ch.hearc.coursJava.GUI.frame.tools.JFrameBase;

public class JFramePreference extends JFrameBase
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public JFramePreference()
		{
		addListener();
		setPropriete();
		loadPreferences();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void addListener()
		{
		addWindowListener(new WindowAdapter()
			{

				@Override
				public void windowClosing(WindowEvent arg0)
					{
					System.out.println("Window Closing");
					savePreferences();
					System.out.println("Preferences sauves");
					System.exit(0); // 0 normal, -1 anormal
					}
			});
		}

	private void loadPreferences()
		{
		//deuxieme parametre correspond a la valeur par defaut, si la cle nexiste pas encore dans la base de registre
		int x0 = PREFERENCES.getInt("x0", 0);
		int y0 = PREFERENCES.getInt("y0", 0);
		int width = PREFERENCES.getInt("width", 800);
		int height = PREFERENCES.getInt("height", 600);

		this.setLocation(new Point(x0, y0));
		this.setSize(width, height);
		}

	private void savePreferences()
		{
		PREFERENCES.putInt("x0", (int)this.getLocation().getX());
		PREFERENCES.putInt("y0", (int)this.getLocation().getY());
		PREFERENCES.putInt("width", this.getWidth());
		PREFERENCES.putInt("height", this.getHeight());
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	private static final Preferences PREFERENCES = Preferences.userNodeForPackage(JFramePreference.class);
	}
